package com.example.bookstore.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="CreditCardConverter")
public class CreditCardConverter implements Converter {

	public static final String CONVERSION_ERROR_MESSAGE_ID = "ConversionError";

	/**
	 * Parses the CreditCardNumber and strips any blanks or <code>"-"</code>
	 * characters from it.
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String newValue) throws ConverterException {
		String convertedValue = null;

		if (newValue == null) {
			return newValue;
		}

		// Since this is only a String to String conversion, this conversion
		// does not throw ConverterException.
		convertedValue = newValue.trim();

		if (((convertedValue.contains("-")) == false)
				|| ((convertedValue.contains(" ")) == false)) {
			char[] input = convertedValue.toCharArray();
			StringBuffer buffer = new StringBuffer(input.length);

			for (int i = 0; i < input.length; ++i) {
				if ((input[i] == '-') || (input[i] == ' ')) {
					continue;
				} else {
					buffer.append(input[i]);
				}
			}

			convertedValue = buffer.toString();
		}

		return convertedValue;
	}

	/**
	 * Formats the value by inserting space after every four characters for
	 * better readability if they don't already exist. In the process converts
	 * any <code>"-"</code> characters into blanks for consistency.
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {
		String inputVal = null;

		if (value == null) {
			return null;
		}

		// value must be of the type that can be cast to a String.
		try {
			inputVal = (String) value;
		} catch (ClassCastException ce) {
			// FacesMessage errMsg = MessageFactory.getMessage(
			// CONVERSION_ERROR_MESSAGE_ID, (new Object[] { value,
			// inputVal }));
			throw new ConverterException(ce.getMessage());
		}

		// insert spaces after every four characters for better
		// readability if it doesn't already exist.
		char[] input = inputVal.toCharArray();
		StringBuffer buffer = new StringBuffer(input.length + 3);

		for (int i = 0; i < input.length; ++i) {
			if (((i % 4) == 0) && (i != 0)) {
				if ((input[i] != ' ') || (input[i] != '-')) {
					buffer.append(" ");

					// if there any "-"'s convert them to blanks.
				} else if (input[i] == '-') {
					buffer.append(" ");
				}
			}

			buffer.append(input[i]);
		}

		String convertedValue = buffer.toString();

		return convertedValue;
	}
}
